package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.Equipment;
import com.hgxd.entity.pojo.Slave;
import com.hgxd.entity.pojo.SlaveHistory;
import com.hgxd.entity.vo.ElectricRoomVo;
import com.hgxd.exception.ServiceException;
import com.hgxd.result.Result;
import com.hgxd.service.IEquipmentService;
import com.hgxd.service.ISlaveHistoryService;
import com.hgxd.service.ISlaveService;
import com.hgxd.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wtf
 * @create 2023-11-20 15:45
 * @description : 电气室状态相关
 */
@Api(tags = "电气室状态相关")
@RestController
@RequestMapping("/api/electricRoom")
@CrossOrigin
public class ElectricRoomController {

    @Resource(name = "SlaveHistoryServiceImpl")
    private ISlaveHistoryService slaveHistoryService;

    @Resource(name = "EquipmentServiceImpl")
    private IEquipmentService equipmentService;

    @Resource(name = "SlaveServiceImpl")
    private ISlaveService slaveService;

    /**
     * 根据从机id和时间区间查询对应最新数据
     *
     * @param electricRoomVo
     * @return
     */
    @PostMapping("getNewSlaveHistory")
    @ApiOperation(value = "根据从机id和时间区间查询对应最新数据", notes = "传入指定柜号")
    @OperationLogAnnotation(operModule = "从机变量记录管理-根据从机id和时间区间查询对应最新数据", operType = "查询", operContent = "根据从机id和时间区间查询对应最新数据")
    public Result<SlaveHistory> getNewSlaveHistory(@RequestBody ElectricRoomVo electricRoomVo) {
        LambdaQueryWrapper<SlaveHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(electricRoomVo.getSlaveId()>=0, SlaveHistory::getSlaveId, electricRoomVo.getSlaveId())
                .between(SlaveHistory::getUpdateTime,electricRoomVo.getBeginTime(),electricRoomVo.getEndingTime())
                .orderByDesc(SlaveHistory::getUpdateTime);
        List<SlaveHistory> list = slaveHistoryService.list(queryWrapper);
        return list.isEmpty() ? Result.ok(new SlaveHistory()) : Result.ok(list.get(0));
    }

    /**
     * 根据organizationId获取设备的数量和在线数量
     *
     * @param organizationId
     * @return
     */
    @ApiOperation(value = "根据organizationId获取设备的数量和在线数量", notes = "传入对应组织id(organizationId)")
    @OperationLogAnnotation(operModule = "从机变量记录管理-根据organizationId获取设备的数量和在线数量", operType = "查询", operContent = "根据organizationId获取设备的数量和在线数量")
    @GetMapping("/getEquipmentNumber/{organizationId}")
    public Result<Map<String, Integer>> getEquipmentNumber(@PathVariable("organizationId") Integer organizationId) {
        LambdaQueryWrapper<Equipment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Equipment::getOrganizationId, organizationId);
        int total = (int) equipmentService.count(queryWrapper);
        queryWrapper.eq(Equipment::getEquipmentStatus, 0);
        int count = (int) equipmentService.count(queryWrapper);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("total", total);
        map.put("count", count);
        return Result.ok(map);
    }

    /**
     * 根据柜号查询电流电压等信息
     *
     * @param cntrNo
     * @return
     */
    @GetMapping("/getSlaveMessage/{cntrNo}")
    @ApiOperation(value = "根据柜号查询电流电压等信息", notes = "传入对应柜号(cntrNo)")
    @OperationLogAnnotation(operModule = "从机变量记录管理-根据柜号查询电流电压等信息", operType = "查询", operContent = "根据柜号查询电流电压等信息")
    public Result<List<SlaveHistory>> getSlaveMessage(@PathVariable("cntrNo") String cntrNo) {

        //TODO 勿删
//        LocalDateTime now = LocalDateTime.now();
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        String format = now.format(formatter);
//        format = format + " 00:00:00";
//        LocalDateTime time = DateUtil.stringToTime(format);
//        LocalDateTime startTime = time.plusDays(1).plusSeconds(-1);
//        LocalDateTime endingTime = time.plusDays(-7);
        //TODO 上线删除
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        String startString = "2023-01-01 00:00:01";
        String endingString = "2023-12-31 23:59:59";
        LocalDateTime startTime = DateUtil.stringToTime(startString);
        LocalDateTime endingTime = DateUtil.stringToTime(endingString);

        LambdaQueryWrapper<SlaveHistory> queryWrapper = new LambdaQueryWrapper<SlaveHistory>()
                .eq(SlaveHistory::getCntrNo, cntrNo)
                .between(SlaveHistory::getUpdateTime, startTime, endingTime)
                .orderByDesc(SlaveHistory::getUpdateTime);
        return Result.ok(slaveHistoryService.list(queryWrapper));
    }

    /**
     * 根据organizationId获取从机的数量和在线数量
     *
     * @param organizationId
     * @return
     */
    @ApiOperation(value = "根据organizationId获取从机的数量和在线数量", notes = "传入对应组织id(organizationId) 传入101")
    @OperationLogAnnotation(operModule = "从机变量记录管理-根据organizationId获取从机的数量和在线数量", operType = "查询", operContent = "根据organizationId获取从机的数量和在线数量")
    @GetMapping("/getSlaveNumber/{organizationId}")
    public Result<Map<String, Integer>> getSlaveNumber(@PathVariable("organizationId") Integer organizationId) {
        LambdaQueryWrapper<Slave> slaveQueryWrapper = new LambdaQueryWrapper<>();
        slaveQueryWrapper.eq(Slave::getOrganizationId, organizationId);
        int total = (int) slaveService.count(slaveQueryWrapper);
        slaveQueryWrapper.eq(Slave::getStatus, 0);
        int count = (int) slaveService.count(slaveQueryWrapper);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("total", total);
        map.put("count", count);
        return Result.ok(map);
    }

    /**
     * 查询某天每小时某从机总电能
     *
     * @param electricRoomVo
     * @return
     */
    @PostMapping("/getTotalEnergyByHour")
    @ApiOperation(value = "查询某天每小时某从机总电能", notes = "传入对应从机id和当天时间，例如：beginTime:2023-07-05 00:00:01 endingTime:2023-07-05 23:59:59 此为一天时间")
    @OperationLogAnnotation(operModule = "从机变量记录管理-查询某天每小时某从机总电能", operType = "查询", operContent = "查询某天每小时某从机总电能")
    public Result<Map<Integer, Double>> getTotalEnergyByHour(@RequestBody ElectricRoomVo electricRoomVo) {
        return Result.ok(slaveHistoryService.getTotalEnergyByHour(electricRoomVo.getBeginTime(), electricRoomVo.getEndingTime(), electricRoomVo.getSlaveId()));
    }


}
